﻿package com.saint.utils {
	
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldType;
	import flash.text.TextFieldAutoSize;
	import flash.text.Font;
	
	public class TextFactory extends Sprite {
		
		public static const DEFAULT:String = "default";
		
		private var FONT_ARIAL:String = "Arial";
		private var FONT_COLOUR_BLACK:uint = 0x000000;
		
		private var _txt:TextField;
		private var fmt_txt:TextFormat;
		
		public function TextFactory(str:String = "", fmt:String = DEFAULT):void {
			_txt = new TextField();
			_txt.height = 20;
			fmt_txt = new TextFormat();
			buildFormat(fmt);
			
			addChild(_txt);
			txt = str;
		}
		
		private function buildFormat(type:String):void{
			var fontName:String;
			var fontSize:uint;
			var fontColour:uint;
			var fontItalic:Boolean = false;
			var fontBold:Boolean = false;
			switch(type){
				case DEFAULT:
				default:
					fontName = FONT_ARIAL;
					fontSize = 10;
					fontColour = FONT_COLOUR_BLACK;
					setFieldProperties( {} );
			}
			fmt_txt.font = fontName;
			fmt_txt.size = fontSize;
			fmt_txt.color = fontColour;
			fmt_txt.italic = fontItalic;
			fmt_txt.bold = fontBold;
		}
		
		private function setFieldProperties(o:Object):void {
			o.autoSize != null		?	_txt.autoSize = o.autoSize		:	_txt.autoSize = TextFieldAutoSize.LEFT;
			o.embedFonts != null	?	_txt.embedFonts = o.embedFonts	:	_txt.embedFonts = true;
			o.wordWrap != null		?	_txt.wordWrap = o.wordWrap		:	_txt.wordWrap = true;
			o.multiline != null		?	_txt.multiline = o.multiline	:	_txt.multiline = true;
			o.selectable != null	?	_txt.selectable = o.selectable	:	_txt.selectable = false;
			o.type != null			?	_txt.type = o.type				:	_txt.type = TextFieldType.DYNAMIC;
		}
		
		private function applyFormatting():void {
			_txt.defaultTextFormat = fmt_txt;
			_txt.setTextFormat(fmt_txt);
		}
		
/**
 * public functions
 */
		public function setWidth(w:uint):void{
			_txt.width = w;
		}
		
		public function setGetURL(link:String, window:String = "_blank") {
			fmt_txt.url = link;
			fmt_txt.target = window;
			_txt.setTextFormat(fmt_txt);
		}
		
/**
 * getter and setters
 */
		public function set format(type:String):void {
			buildFormat(type);
			applyFormatting();
		}
		public function get txt():String{
			return _txt.text;
		}
		
		public function set txt(str:String):void{
			_txt.text = str;
			applyFormatting();
		}
		
		public function get htmlTxt():String{
			return _txt.htmlText;
		}
		
		public function set htmlTxt(str:String):void{
			_txt.htmlText = str;
			applyFormatting();
		}
		
	}
}